import useUserStore from '@/stores/user'

const userStore = useUserStore()

export default function useApply() {
  const router = useRouter()
  const applyProduct = (productInfo: object) => {
    if (!userStore.currEnterprise) {
      ElMessage.warning('企业信息不存在，认证后申请')
      return
    }
    if (userStore.currEnterprise.examineResult != '2') {
      ElMessage.warning('请先完成企业实名认证')
      return
    }

    router.push({
      name: 'needsFormPage',
      query: {
        productId: productInfo.id,
        organizationName: productInfo.belongInstitutionName,
        productName: productInfo.productName,
      },
    })
  }

  const clearCache = () => {
    localStorage.removeItem('productInfo')
  }

  const getData = () => {
    const productInfo = localStorage.getItem('productInfo')
    return productInfo ? JSON.parse(productInfo) : null
  }

  return {
    applyProduct,
    clearCache,
    getData,
  }
}
